using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UsrControls
{

    public class RadioButtonListRequiredFieldValidator : BaseValidator
    {


        protected override bool ControlPropertiesValid()
        {
            Control ctrl = FindControl(ControlToValidate);
            if (ctrl != null)
            {
                CheckBoxList _listctrl = (CheckBoxList)ctrl;
                return (_listctrl != null);
            }
            else
                return false;

        }


        protected override bool EvaluateIsValid()
        {

            return this.EvaluateIsChecked();

        }



        protected bool EvaluateIsChecked()
        {

            CheckBoxList _cbl = ((CheckBoxList)this.FindControl(this.ControlToValidate));

            foreach (ListItem li in _cbl.Items)
            {

                if (li.Selected == true)
                {

                    return true;

                }

            }
            return false;


        }

        protected override void OnPreRender(EventArgs e)
        {

            if (this.EnableClientScript) { this.ClientScript(); }
            base.OnPreRender(e);

        }

        protected void ClientScript()
        {
            this.Attributes["evaluationfunction"] = "cb_vefify";

            StringBuilder sb_Script = new StringBuilder();
            sb_Script.Append("<script language=\"javascript\">");
            sb_Script.Append("\r");
            sb_Script.Append("\r");
            sb_Script.Append("function cb_vefify(val) {");
            sb_Script.Append("\r");
            sb_Script.Append("var val = document.all[document.all[\"");
            sb_Script.Append(this.ClientID);
            sb_Script.Append("\"].controltovalidate];");
            sb_Script.Append("\r");
            sb_Script.Append("var col = val.all;");
            sb_Script.Append("\r");
            sb_Script.Append("if ( col != null ) {");
            sb_Script.Append("\r");
            sb_Script.Append("for ( i = 0; i < col.length; i++ ) {");
            sb_Script.Append("\r");
            sb_Script.Append("if (col.item(i).tagName == \"INPUT\") {");
            sb_Script.Append("\r");
            sb_Script.Append("if ( col.item(i).checked ) {");
            sb_Script.Append("\r");
            sb_Script.Append("\r");
            sb_Script.Append("return true;");
            sb_Script.Append("\r");
            sb_Script.Append("}");
            sb_Script.Append("\r");
            sb_Script.Append("}");
            sb_Script.Append("\r");
            sb_Script.Append("}");
            sb_Script.Append("\r");
            sb_Script.Append("\r");
            sb_Script.Append("\r");
            sb_Script.Append("return false;");
            sb_Script.Append("\r");
            sb_Script.Append("}");
            sb_Script.Append("\r");
            sb_Script.Append("}");
            sb_Script.Append("\r");
            sb_Script.Append("</script>");
            this.Page.RegisterClientScriptBlock("RBLScript", sb_Script.ToString());
        }

    }

}
